;
(function() {
  window.authConfig = new Vue({
    el: '#auth-config-pop',
    data: {
      tab: [{
        'name': '管理端PC',
        'code': 'BGPC',
        'selected': true
      }, {
        'name': '管理端H5',
        'code': 'BGYD',
        'selected': false
      }, {
        'name': '用户端PC',
        'code': 'YHPC',
        'selected': false
      }],
      curPlatform: 'BGPC',
      positionId: null,
      firstMenu: [],
      selectedFirstMenu: {},
      secondMenu: [],
      selectedFirstPrivilege: [],
      selectedSecondPrivilege: [],
      activeIndex: 0,
      allFirstPrivilege: {
        'BGPC': ['c1', 'c3', 'c26', 'c30', 'c36', 'c52', 'c42', 'c72', 'c82', 'c271'],
        'BGYD': ['c1', 'c26', 'c52', 'c42', 'c289', 'c36'],
        'YHPC': ['c259','c189'],
      },
      isShowAuthFlag: false
    },
    methods: {
      selectPlatform(i) {
        if (this.tab[i]['selected'] == true) return false;
        this.curPlatform = this.tab[i]['code'];
        this.tab.forEach((el, index) => {
          if (index == i) el['selected'] = true;
          if (index != i) el['selected'] = false;
        })
        this.clearData()
        this.getFirstMenuData()
        this.getPositionPrivilege()
      },
      // 获取该岗位下所拥有的权限点
      getPositionPrivilege() {
        var self = this;
        hlhttp.get({
          url: 'position/' + self.positionId + '/privileges',
          data: {
            'platform': self.curPlatform
          },
          success: function(data) {
            if (data.length === 0) return false;
            // 重分一级权限点和二级权限点
            data.forEach((_item, _index) => {
              if (self.curPlatform == 'BGPC') {
                if (self.allFirstPrivilege['BGPC'].indexOf(_item) >= 0) self.selectedFirstPrivilege.push(_item)
                if (self.allFirstPrivilege['BGPC'].indexOf(_item) < 0) self.selectedSecondPrivilege.push(_item)
              }
              if (self.curPlatform == 'BGYD') {
                if (self.allFirstPrivilege['BGYD'].indexOf(_item) >= 0) self.selectedFirstPrivilege.push(_item)
                if (self.allFirstPrivilege['BGYD'].indexOf(_item) < 0) self.selectedSecondPrivilege.push(_item)
              }
              if (self.curPlatform == 'YHPC') {
                if (self.allFirstPrivilege['YHPC'].indexOf(_item) >= 0) self.selectedFirstPrivilege.push(_item)
                if (self.allFirstPrivilege['YHPC'].indexOf(_item) < 0) self.selectedSecondPrivilege.push(_item)
              }
            })
          }
        })
      },
      // 获取对应平台下的一级权限点
      getFirstMenuData() {
        var self = this;
        hlhttp.get({
          url: 'res/0/res,privileges',
          data: {
            'res_id': '0',
            'platform': self.curPlatform
          },
          success: function(data) {
            var arr = [];
            if (data.length) {
              data.forEach((_item, _index) => {
                var obj = JSON.parse(JSON.stringify(_item))
                if (self.curPlatform == 'BGPC') obj['icon'] = self.menuNameMatchBGPCIcon(_item.name)
                if (self.curPlatform == 'BGYD') obj['icon'] = self.menuNameMatchBGYDIcon(_item.name)
                if (self.curPlatform == 'YHPC') obj['icon'] = self.menuNameMatchYHPCIcon(_item.name)
                arr.push(obj)
              })
              self.firstMenu = arr;
              self.handleSelectedFirstMenu(arr[0], 0)
            }
          }
        })
      },
      getSecondMenuData(id) {
        var self = this;
        hlhttp.get({
          url: 'res/' + id + '/res,privileges',
          data: {
            'res_id': id,
            'platform': self.curPlatform
          },
          success: function(data) {
            var arr = [];
            if (data.length) {
              data.forEach(_item => arr.push(_item))
            }

            self.secondMenu = arr;
          }
        })
      },
      handleSelectedFirstMenu(val, i) {
        // 提取出选中的一级菜单数据  调取二级菜单数据
        this.selectedFirstMenu = val;
        this.activeIndex = i;
        this.getSecondMenuData(val.id)
      },
      // 勾选一级目录
      selectFirstAuthItem(id) {
        const index = this.selectedFirstPrivilege.indexOf(id);
        if (index == -1) {
          this.selectedFirstPrivilege.push(id)
          return false;
        }
        if (index > -1) {
          this.selectedFirstPrivilege.splice(index, 1)
          return false;
        }
      },
      // 勾选二级目录
      selectSecondAuthItem(id) {
        const index = this.selectedSecondPrivilege.indexOf(id);
        if (index == -1) {
          this.selectedSecondPrivilege.push(id)
          return false;
        }
        if (index > -1) {
          this.selectedSecondPrivilege.splice(index, 1)
          return false;
        }
      },
      // 反选一级目录
      inverseFirstAuthItem() {
        const privilegesDom = Array.prototype.slice.call(this.$refs.firstPrivilege)
        const privilegesId = [];
        privilegesDom.forEach(el => {
          privilegesId.push(el.getAttribute('id'))
        })
        privilegesId.forEach(el => {
          let index = this.selectedFirstPrivilege.indexOf(el)
          if (index == -1) {
            this.selectedFirstPrivilege.push(el)
            return false;
          }
          if (index > -1) {
            this.selectedFirstPrivilege.splice(index, 1)
            return false;
          }
        })
      },
      // 反选二级目录
      inverseSecondAuthItem(event) {
        const privilegesDom = Array.prototype.slice.call($(event.currentTarget.parentNode).find('.auth-item'))
        const privilegesId = [];
        privilegesDom.forEach(el => {
          privilegesId.push(el.getAttribute('id'))
        })
        privilegesId.forEach(el => {
          let index = this.selectedSecondPrivilege.indexOf(el)
          if (index == -1) {
            this.selectedSecondPrivilege.push(el)
            return false;
          }
          if (index > -1) {
            this.selectedSecondPrivilege.splice(index, 1)
            return false;
          }
        })
      },
      configPositionPrivilege() {
        var self = this;
        var privilegeCodes = this.selectedFirstPrivilege.concat(this.selectedSecondPrivilege).join()

        hlhttp.put({
          url: 'assign_privilege_to_position',
          data: {
            'platform': self.curPlatform,
            'position_id': self.positionId,
            'privilege_ids': privilegeCodes || '#'
          },
          success: function(data) {
            hl.toast('保存成功')
            self.isShowAuthFlag = false;
            self.clearData()
            loginPrem.loginList()
            $('.backdrop, .js-accredit-show').hide()

          }
        })
      },
      menuNameMatchBGPCIcon(name) {
        var res;
        switch (name) {
          case '首页':
            res = 'icon-Nav-home';
            break;
          case '配置':
            res = 'icon-Nav-system';
            break;
          case '租控管理':
            res = 'icon-Nav-lease';
            break;
          case '合同管理':
            res = 'icon-Nav-contract';
            break;
          case '财务管理':
            res = 'icon-Nav-finance';
            break;
          case '管理驾驶舱':
            res = 'icon-Nav-report';
            break;
          case '招商管理':
            res = 'icon-Nav-investment';
            break;
          case 'BI报表':
            res = 'icon-Nav-form';
            break;
          case '资源规划':
            res = 'icon-Nav-planning';
            break;
          case '信息发布':
            res = 'icon-issue';
            break;
          case '市场拓展管理':
            res = 'icon-Nav-markting';
            break;
          default:
        }
        return res;
      },
      menuNameMatchBGYDIcon(name) {
        var res;
        switch (name) {
          case '工作台':
            res = 'icon-Nav-home';
            break;
          case '租赁管理':
            res = 'icon-Nav-lease';
            break;
          case '管理驾驶舱':
            res = 'icon-Nav-report';
            break;
          case '招商管理':
            res = 'icon-Nav-investment';
            break;
          case '信息发布':
            res = 'icon-issue';
            break;
          case '市场拓展管理':
            res = 'icon-Nav-markting';
            break;
          case '财务管理':
            res = 'icon-Nav-finance';
            break;
          default:
        }
        return res;
      },
      menuNameMatchYHPCIcon(name) {
        var res;
        switch (name) {
          case '信息发布':
            res = 'icon-issue';
            break;
          default:
        }
        return res;
      },
      showPop() {
        var isSelectedPosition = false;
        var self = this;

        $('.js-accredit-btn').click(() => {
          $('.ts-position-item').each((i, el) => {
            if ($(el).hasClass('active')) {
              isSelectedPosition = true;
              self.positionId = $(el).attr('data-id');
            }
          })

          if (isSelectedPosition == false) hl.toast('请先选择岗位再授权权限');
          if (isSelectedPosition == true) {
            self.getFirstMenuData()
            self.getPositionPrivilege()
            self.isShowAuthFlag = true;
            $('.backdrop').show()
          }
        })
      },
      hidePop() {
        this.isShowAuthFlag = false;
        this.clearData()
        $('.backdrop').hide()
      },
      clearData() {
        this.selectedFirstPrivilege = [];
        this.selectedSecondPrivilege = [];
        this.firstMenu = [];
        this.secondMenu = [];
        this.selectedFirstMenu = {};
      }
    },
    created() {
      this.getFirstMenuData()
      this.showPop()
    }
  })

}())
